package org.enate.bloatterrorist;

import org.enate.bloatterrorist.AppListFragment.GridItemsAdapter;
import org.enate.bloatterrorist.AppListFragment.reloadGridView;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.pm.PackageManager;
import android.graphics.drawable.TransitionDrawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.Toast;

public final class Main extends FragmentActivity {
	private static final String TAG = "Main";
	protected static Context c;
	protected static Main m = null;
	protected static SharedPreferences prefs = null;
	protected static Editor prefs_editor = null;
	protected static PackageManager pm;
	protected static InputMethodManager imm;
	
	
	private TransitionDrawable mTitleLogo;
	protected static LayoutInflater li = null;
	protected static ProgressDialog pd;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);	
		setContentView(R.layout.main);
		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		prefs_editor = prefs.edit();
		m = Main.this;
		c = getApplicationContext();
		pm = getPackageManager();
		li = getLayoutInflater();
		imm = (InputMethodManager) c.getSystemService(Context.INPUT_METHOD_SERVICE);
		API.checkVurguning();
		PagerAdapter.buildPA();
		ActionItem.build_APF_AI();
		mTitleLogo = (TransitionDrawable) ((ImageView)Main.m.findViewById(R.id.title_logo)).getDrawable();
    	if(!API.isRooted()) {makeToast("Your device isn't rooted. Only limited options available.");    	}
	}

	@Override protected void onDestroy() {
		super.onDestroy();
    	try{imm.hideSoftInputFromWindow(FilterFragment.et.getWindowToken(), 0);}catch(Exception e){}	
	}
	@Override protected void onResume(){
		super.onResume();
		try{imm.hideSoftInputFromWindow(FilterFragment.et.getWindowToken(), 0);}catch(Exception e){}
	}
	@Override protected void onPause(){
		try{imm.hideSoftInputFromWindow(FilterFragment.et.getWindowToken(), 0);}catch(Exception e){}
		super.onPause();
	}
	
	protected static void makeToast(String message) {
		Toast.makeText(Main.c, message, Toast.LENGTH_LONG).show();
	}

	public boolean onPrepareOptionsMenu(Menu menu) {
		menu.clear();
		menu.add(Menu.NONE, 1, Menu.NONE, "App Home").setIcon(R.drawable.home);
		menu.add(Menu.NONE, 2, Menu.NONE, "Refresh").setIcon(R.drawable.refresh);
		menu.add(Menu.NONE, 3, Menu.NONE, "Preferences").setIcon(R.drawable.prefs);
		menu.add(Menu.NONE, 4, Menu.NONE, "Help").setIcon(R.drawable.help);
		/*
		switch(Main.prefs.getInt(API.str_prefShowJustLaunchables, 1)) {
		case 0: menu.add(Menu.NONE, 2, Menu.NONE, "Show Launcher Only").setIcon(R.drawable.toggle_launchonly); break;
		case 1: menu.add(Menu.NONE, 2, Menu.NONE, "Show All Apps").setIcon(R.drawable.toggle_all); break;
		}
		*/
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch(item.getItemId()) {
		case 1: PagerAdapter.mPager.setCurrentItem(1); break;
		case 2:
			AppListFragment.installedAppsCache = null;
	        AppListFragment.lia = new GridItemsAdapter(AppListFragment.installedAppsCache);
	        AppListFragment.rgv = new reloadGridView();
	        AppListFragment.rgv.execute(AppListFragment.mFilter);
			break;
		case 3: PagerAdapter.mPager.setCurrentItem(2); break;
		case 4: PagerAdapter.mPager.setCurrentItem(4); break;
		}
		return true;
	}
	
	@Override protected void onSaveInstanceState(Bundle outState) {super.onSaveInstanceState(outState);}
    @Override protected void onRestoreInstanceState(Bundle outState) {super.onRestoreInstanceState(outState);}

    
}